using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using EventUtils;
using UnityEngine;
using UnityEngine.UI;
using EventType = EventUtils.EventType;

namespace View.Instant
{
    public class LobbyView : View
    {
        private GameObject mobjPanel1;
        private Button mbtnGoLobby;
        
        private GameObject mobjPanel2;
        private Button mbtnSet;
        private Button mbtnAddGold;
        private Button mbtnPower;
        private Button mbtnLucky;
        private Button mbtnTask;
        private Button mbtnRanking;
        private Button mbtnShop;
        private Button mbtnAddPower;

        private Text mtxtGold;
        private Text mtxtPower;
        
        private Button mbtnGame1;
        private Button mbtnGame2;
        public override void OnLoad()
        {
            // PlayerPrefs.DeleteKey("CUR_TYPE_LEVEL_1_1");
            // PlayerPrefs.DeleteKey("CUR_TYPE_LEVEL_2_1");
            base.OnLoad();
            
            // PlayerPrefs.DeleteAll();
            OnInit();
            OnEvent();
            InitData();

            GameLocalInfo.OnSetDepotPowerTime();
            GameLocalInfo.OnGetLocalPowerTimer(true);
            GameLocalInfo.OnSetSelfItemToLocal(true);
            
            GameLocalInfo.OnLoadPropIconTable();
            TaskManager.OnLoadLocalTask();
            TaskManager.OnGetLocalIsSameDay(true);

            TaskManager.OnAddTaskNumByTaskType(TaskTypeEnum.login);
            UIManager.Instance.Show(ViewName.PublicEventCenter, ViewLayer.Top);

            // AdvCtrlManager.LoadBanner();//加载banner广告
            AndroidManager.Ins.LoadBanner();
            EventCenter.AddListener(EventType.EVNET_UPDATE_PLAYERINFO,OnUpdatePlayerInfo);
        }

        private void OnInit()
        {
            mobjPanel1 = transform.Find("Panel1").gameObject;
            mbtnGoLobby = transform.Find("Panel1/btnGo").GetComponent<Button>();

            mobjPanel2 = transform.Find("Panel2").gameObject;
            mbtnSet = transform.Find("Panel2/btnset").GetComponent<Button>();
            mbtnAddGold = transform.Find("Panel2/btnaddGold").GetComponent<Button>();
            mbtnPower = transform.Find("Panel2/btnPower").GetComponent<Button>();
            mbtnLucky = transform.Find("Panel2/btnLucky").GetComponent<Button>();
            mbtnTask = transform.Find("Panel2/btnTask").GetComponent<Button>();
            mbtnRanking = transform.Find("Panel2/btnRanking").GetComponent<Button>();
            mbtnShop = transform.Find("Panel2/btnShop").GetComponent<Button>();
            mbtnAddPower = transform.Find("Panel2/btnAddPower").GetComponent<Button>();
            
            mtxtGold = transform.Find("Panel2/btnaddGold/txtGold").GetComponent<Text>();
            mtxtPower = transform.Find("Panel2/btnPower/txtPower").GetComponent<Text>();
            
            mbtnGame1 = transform.Find("Panel2/btngame1").GetComponent<Button>();
            mbtnGame2 = transform.Find("Panel2/btngame2").GetComponent<Button>();
        }
        
        private void OnEvent()
        {
            AddClickListener(mbtnGoLobby, () =>
            {
                mobjPanel1.SetActive(false);
                mobjPanel2.SetActive(true);
            });
            
            AddClickListener(mbtnSet, () =>
            {
                UIManager.Instance.Show(ViewName.SettingView, ViewLayer.Middle);
            });
            
            AddClickListener(mbtnAddGold, () =>
            {
                UIManager.Instance.Show(ViewName.ShopView, ViewLayer.Middle);
            });
            AddClickListener(mbtnPower, () =>
            {
                UIManager.Instance.Show(ViewName.DepotPowerView, ViewLayer.Middle);
            });
            AddClickListener(mbtnLucky, () =>
            {
                UIManager.Instance.Show(ViewName.LuckyView, ViewLayer.Middle);
            });
            AddClickListener(mbtnTask, () =>
            {
                UIManager.Instance.Show(ViewName.TaskView, ViewLayer.Middle);
            });
            AddClickListener(mbtnRanking, () =>
            {
                // UIManager.Instance.Show(ViewName.SettingView, ViewLayer.Middle);
            });
            AddClickListener(mbtnShop, () =>
            {
                UIManager.Instance.Show(ViewName.ShopView, ViewLayer.Middle);
            });
            AddClickListener(mbtnAddPower, () =>
            {
                UIManager.Instance.Show(ViewName.DepotPowerView, ViewLayer.Middle);
            });
            
            AddClickListener(mbtnGame1,() =>
            {
                if (GameLocalInfo.mPlayerPower <= 0)
                {
                    UIManager.Instance.Show(ViewName.ToolTipsView, ViewLayer.Top, "体力不足！");
                }
                else
                {
                    GameHelp.GameType = 1;
                    UIManager.Instance.Close(ViewName.LobbyView);
                    GameLocalInfo.OnUpdatePlayerPower(-1);
                    UIManager.Instance.Show(ViewName.Game1View,ViewLayer.Game); 
                }
            });
            AddClickListener(mbtnGame2,() =>
            {
                if (GameLocalInfo.mPlayerPower <= 0)
                {
                    UIManager.Instance.Show(ViewName.ToolTipsView, ViewLayer.Top, "体力不足！");
                }
                else
                {
                    GameHelp.GameType = 2;
                    UIManager.Instance.Close(ViewName.LobbyView);
                    GameLocalInfo.OnUpdatePlayerPower(-1);
                    UIManager.Instance.Show(ViewName.Game2View,ViewLayer.Game); 
                }
            });
            
            
        }

        private void InitData()
        {
            AudioSystem.Instance.OnPlayBGM();
            if (GameLocalInfo.OnIsPlayBGM())
            {
                AudioSystem.Instance.ResumeBgm();
            }
            else
            {
                AudioSystem.Instance.PauseBgm();
            }
            
            bool isplayeffect = GameLocalInfo.OnIsPlayEffect();
            AudioSystem.Instance.SetEffectAudioVolume(isplayeffect?1:0);

            GameLocalInfo.mPlayerGold = GameLocalInfo.OnGetLocalGold();
            GameLocalInfo.mPlayerPower = GameLocalInfo.OnGetLocalPower();
            OnUpdatePlayerInfo();

            GameLocalInfo.mLoginLuckyTimer = GameLocalInfo.OnGetNowTimeToLucky();
        }

        private void OnUpdatePlayerInfo()
        {
            try
            {
                float goldf = float.Parse(mtxtGold.text);
                float powerf = float.Parse(mtxtPower.text);

                DOTween.To(() => goldf, x => mtxtGold.text = ((int)x).ToString(), GameLocalInfo.mPlayerGold, 0.2f);
                DOTween.To(() => powerf, x => mtxtPower.text = ((int)x).ToString(), GameLocalInfo.mPlayerPower, 0.2f);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                mtxtGold.text = GameLocalInfo.mPlayerGold.ToString();
                mtxtPower.text = GameLocalInfo.mPlayerPower.ToString();
            }
            
            Debug.LogError("当前体力"+GameLocalInfo.mPlayerPower);
        }

        public override void OnShow(params object[] args)
        {
            base.OnShow();
            Utils.OnUpdate += Update;
            // EventCenter.AddListener(EventType.EVNET_UPDATE_PLAYERINFO,OnUpdatePlayerInfo);
            OnUpdatePlayerInfo();
        }

        public override void OnClose(params object[] args)
        {
            base.OnClose();
            if (gameObject.activeSelf)
            {
                Utils.OnUpdate -= Update;
                // EventCenter.RemoveListener(EventType.EVNET_UPDATE_PLAYERINFO,OnUpdatePlayerInfo);
            }
        }

        private void Update(float timer)
        {
            GameLocalInfo.OnDepotPowerTimeSub();
        }
    }
}
